package com.example.newsapp.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.newsapp.activity.HistoryActivity;
import com.example.newsapp.activity.LovedActivity;
import com.example.newsapp.activity.MainActivity;
import com.example.newsapp.activity.ProfileActivity;
import com.example.newsapp.R;
import com.example.newsapp.activity.ResetPasswordActivity;
import com.example.newsapp.utils.UserSessionUtil;

public class ProfileFragment extends Fragment {
    private LinearLayout personalInfoLayout;
    private LinearLayout accountSecurityLayout;
    private LinearLayout browsingHistoryLayout;
    private LinearLayout myFavoritesLayout;
    private Button btnLogout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        // 确保使用正确的布局文件
        View view = inflater.inflate(R.layout.fragment_profile, container, false);
        initViews(view);
        setClickListeners();
        return view;
    }

    private void initViews(View view) {
        // 使用正确的ID找到对应的视图
        personalInfoLayout = view.findViewById(R.id.personalInfoLayout);
        accountSecurityLayout = view.findViewById(R.id.accountSecurityLayout);
        browsingHistoryLayout = view.findViewById(R.id.browsingHistoryLayout);
        myFavoritesLayout = view.findViewById(R.id.myFavoritesLayout);
        btnLogout = view.findViewById(R.id.btnLogout);
    }

    private void setClickListeners() {
        // 个人信息按钮点击事件
        personalInfoLayout.setOnClickListener(v -> {
            Intent intent = new Intent(requireContext(), ProfileActivity.class);
            startActivity(intent);
        });

        // 重置密码按钮点击事件
        accountSecurityLayout.setOnClickListener(v -> {
            Intent intent = new Intent(requireContext(), ResetPasswordActivity.class);
            startActivity(intent);
        });

        // 浏览记录按钮点击事件
        browsingHistoryLayout.setOnClickListener(v -> {
            startActivity(new Intent(requireContext(), HistoryActivity.class));
        });

        // 我的喜欢按钮点击事件
        myFavoritesLayout.setOnClickListener(v -> {
            startActivity(new Intent(requireContext(), LovedActivity.class));
        });

        // 退出登录按钮点击事件
        btnLogout.setOnClickListener(v -> handleLogout());
    }

    //退出登录
    private void handleLogout() {
        // 清除登录状态
        UserSessionUtil.logout(requireContext());

        Toast.makeText(requireContext(), "已退出登录", Toast.LENGTH_SHORT).show();

        // 通知MainActivity更新UI
        if (getActivity() instanceof MainActivity) {
            ((MainActivity) getActivity()).onLogout();
        }
    }
}